package com.fs.ssm004.controller;

import com.fs.ssm004.pojo.Course;
import com.fs.ssm004.pojo.Grade;
import com.fs.ssm004.service.CourseService;
import com.fs.ssm004.service.GradeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/grade")
public class GradeController {
    @Autowired
    private GradeService gradeService;
    @RequestMapping("/getview1")
    public String getListView() {
        return "grademanage";
    }

    @RequestMapping("/getlist")
    @ResponseBody
    public Map<String, Object> getList() {
        List<Grade> gradeList = gradeService.getAll();
        Map<String, Object> result = new HashMap<>();
        System.out.println(gradeList);
        result.put("code", 0);
        result.put("message", "获取成绩数据成功");
        result.put("gradeList",gradeList);
        return result;
    }
    @RequestMapping("/selectLike")
    @ResponseBody
    public Map<String, Object> selectLike(Integer s_id) {
        List<Grade> gradeList = gradeService.selectLike(s_id);
        Map<String, Object> result = new HashMap<>();
        System.out.println(gradeList);
        result.put("code", 0);
        result.put("message", "获取成绩数据成功");
        result.put("gradeList",gradeList);
        return result;
    }

    @RequestMapping("/getListByPage")
    @ResponseBody
    public Map<String, Object> getListByPage(Integer currentPage,
                                             Integer pageSize) {

        PageInfo<Grade> pageInfo = gradeService.getUsersByPage(currentPage, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取成绩数据成功");
        result.put("gradeList", pageInfo.getList()); // 返回查询出来的分页数据
        result.put("totalPage", pageInfo.getPages()); //返回当前总共有多少页
        return result;
    }
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String, Object> getone(Integer id){
        Map<String, Object> result = new HashMap<>();
        Grade grade = gradeService.getoneByCourseId(id);
        if (grade == null) {
            result.put("code", -1);
            result.put("message", "获取用户数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "获取用户数据成功");
            result.put("grade", grade);
        }
        return result;
    }
    @RequestMapping("/deleteOne")
    @ResponseBody
    public Map<String, Object> deleteOne(Integer id){
        boolean modified;
        Map<String, Object> result = new HashMap<>();
        modified = gradeService.removeOne(id);
        if (!modified) {
            result.put("code", -1);
            result.put("message", "编辑课程数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "编辑课程数据成功");
        }
        return result;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(Grade grade) {
        System.out.println(grade);
        Map<String, Object> result = new HashMap<>();
        boolean modified;
        if (grade.getId() > 0) {
            modified = gradeService.modifyOne(grade);
        } else {
            modified = gradeService.addOne(grade);
        }
        if (!modified) {
            result.put("code", -1);
            result.put("message", "编辑课程数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "编辑课程数据成功");
        }
        return result;
    }

}
